package com.ktjy.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

@Service
public class EmailService {
    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 发送邮件
     * @param from
     * @param to
     * @param subject
     * @param text
     * @param cc
     * @throws MessagingException
     */
    public void sendEmail(String from ,String to ,String subject,String text ,String... cc) throws MessagingException {
        if (ObjectUtils.isEmpty(from)) {
            throw new RuntimeException("发送者不能为空");
        }
        if (ObjectUtils.isEmpty(to)) {
            throw new RuntimeException("接收者不能为空");
        }
        MimeMessage mimeMessage=javaMailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);

        helper.setFrom(from); //发件人
        helper.setTo(to); //收件人
        if (cc.length>0 && cc!=null) {
            helper.setCc(cc); //抄送
        }
        helper.setSubject(subject); //主题
        helper.setText(text,true); //内容
        javaMailSender.send(mimeMessage);
    }
}
